home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / iff / gio.c next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  141 lines

  1. /*----------------------------------------------------------------------*
  2.  * GIO.C  Generic I/O Speed Up Package                        01/06/86
  3.  * See GIOCall.C for an example of usage.
  4.  * Read not speeded-up yet.  Only one Write file buffered at a time.
  5.  * Note: The speed-up provided is ONLY significant for code such as IFF
  6.  * which does numerous small Writes and Seeks.
  7.  *----------------------------------------------------------------------*/
  8. #include "gio.h"   /* See comments here for explanation.*/
  9.  
  10. #if GIO_ACTIVE
  11.  
  12. #define local static
  13.  
  14.  
  15. local BPTR wFile = NULL;
  16. local BYTE *wBuffer = NULL;
  17. local LONG wNBytes=0;    /* buffer size in bytes.*/
  18. local LONG wIndex=0;    /* index of next available byte.*/
  19. local LONG wWaterline=0; /* Count of # bytes to be written.
  20.            * Different than wIndex because of GSeek.*/
  21.  
  22. /*----------- GOpen ----------------------------------------------------*/
  23. int GOpen(filename, openmode)
  24.  char *filename;
  25.  int openmode;
  26.  {
  27.     return( Open(filename, openmode) );
  28.  }
  29.  
  30. /*----------- GClose ---------------------------------------------------*/
  31. int GClose(file)
  32.  BPTR file;
  33.  {
  34.     int signal = 0, signal2;
  35.     if (file == wFile)
  36.       signal = GWriteDeclare(NULL, NULL, 0);
  37.     signal2 = Close(file);   /* Call Close even if trouble with write.*/
  38.     if (signal2 < 0)
  39.       signal = signal2;
  40.     return( signal );
  41.  }
  42.  
  43. /*----------- GRead ----------------------------------------------------*/
  44. int GRead(file, buffer, nBytes)
  45.  BPTR file;
  46.  BYTE *buffer;
  47.  int nBytes;
  48.  {
  49.     int signal = 0;
  50.     /* We don't yet read directly from the buffer, so flush it to disk and
  51.      * let the DOS fetch it back. */
  52.     if (file == wFile)
  53.        signal = GWriteFlush(file);
  54.     if (signal >= 0)
  55.        signal = Read(file, buffer, nBytes);
  56.     return( signal );
  57.  }
  58.  
  59. /* ---------- GWriteFlush ----------------------------------------------*/
  60. int GWriteFlush(file)
  61.  BPTR file;
  62.  {
  63.     int gWrite = 0;
  64.     if (wFile != NULL  &&  wBuffer != NULL  &&  wIndex > 0L)
  65.        gWrite = Write(wFile, wBuffer, wWaterline);
  66.     wWaterline = wIndex = 0;   /* No matter what, make sure this happens.*/
  67.     return( gWrite );
  68.  }
  69.  
  70. /* ---------- GWriteDeclare --------------------------------------------*/
  71. int GWriteDeclare(file, buffer, nBytes)
  72.     BPTR file;
  73.     BYTE *buffer;
  74.     LONG nBytes;
  75.     {
  76.        int gWrite = GWriteFlush(wFile);  /* Finish any existing usage.*/
  77.        if ( file==NULL  ||  buffer==NULL  ||  nBytes<=3) {
  78.          wFile = NULL;   wBuffer = NULL;     wNBytes = 0;
  79.        }
  80.        else
  81.        {
  82.          wFile = file;   wBuffer = buffer;   wNBytes = nBytes;
  83.        }
  84.        return( gWrite );
  85.     }
  86.  
  87. /* ---------- GWrite ---------------------------------------------------*/
  88. int GWrite(file, buffer, nBytes)
  89.  BPTR file;
  90.  BYTE *buffer;
  91.  int nBytes;
  92.  {
  93.     int gWrite = 0;
  94.     if (file == wFile  &&  wBuffer != NULL) {
  95.        if (wNBytes >= wIndex + nBytes) {
  96.          /* Append to wBuffer.*/
  97.          movmem(buffer, wBuffer+wIndex, nBytes);
  98.          wIndex += nBytes;
  99.          if (wIndex > wWaterline)
  100.            wWaterline = wIndex;
  101.          nBytes = 0;      /* Indicate data has been swallowed.*/
  102.        }
  103.        else {
  104.          wWaterline = wIndex;     /* We are about to overwrite any
  105.          * data above wIndex, up to at least the buffer end.*/
  106.        gWrite = GWriteFlush(file);  /* Write data out in proper order.*/
  107.        }
  108.    }
  109.     if (nBytes > 0  &&  gWrite >= 0)
  110.        gWrite += Write(file, buffer, nBytes);
  111.     return( gWrite );
  112.  }
  113.  
  114. /* ---------- GSeek ----------------------------------------------------*/
  115. int GSeek(file, position, mode)
  116.  BPTR file;
  117.  int position;
  118.  int mode;
  119.  {
  120.     int gSeek = -2;
  121.     int newWIndex = wIndex + position;
  122.  
  123.     if (file == wFile  &&  wBuffer != NULL) {
  124.        if (mode == OFFSET_CURRENT  &&
  125.          newWIndex >= 0  &&  newWIndex <= wWaterline) {
  126.          wIndex = newWIndex;
  127.          gSeek = 0;      /* Okay */
  128.        }
  129.        else {
  130.        /* Can't optimize it.*/
  131.        gSeek = GWriteFlush(file);
  132.        if (gSeek >= 0)   gSeek = -2;  /* OK so far */
  133.        }
  134.    }
  135.    if (gSeek == -2)
  136.       gSeek = Seek(file, position, mode);
  137.    return( gSeek );
  138.  }
  139.  
  140. #endif GIO_ACTIVE
  141.